<template>
  <div class="divBox">
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="queryForm.imgid" placeholder="请输入id" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button v-if="checkPermi(['imgentity:index:save'])" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
        <el-button v-if="checkPermi(['imgentity:index:delete'])"  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="imgid"
        header-align="center"
        align="center"
        label="图片主键id">
      </el-table-column>
      <el-table-column
        prop="imgname"
        header-align="center"
        align="center"
        label="图片名">
      </el-table-column>
      <el-table-column
        prop="imgpath"
        header-align="center"
        align="center"
        label="图片路径">
      </el-table-column>
      <el-table-column
        prop="imgtype"
        header-align="center"
        align="center"
        label="图片类型(1首页轮播图,2EOS专区)">
      </el-table-column>
      <el-table-column
        prop="imgblock"
        header-align="center"
        align="center"
        label="图片版块，0，首页，1，云玩">
      </el-table-column>
      <el-table-column
        prop="imgforeignkey"
        header-align="center"
        align="center"
        label="图片外键id">
      </el-table-column>
      <el-table-column
        prop="imgadderss"
        header-align="center"
        align="center"
        label="网址">
      </el-table-column>
      <el-table-column
        prop="createtime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="modifytime"
        header-align="center"
        align="center"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text"  v-if="checkPermi(['imgentity:index:update'])" size="small" @click="addOrUpdateHandle(scope.row.imgid)">修改</el-button>
          <el-button type="text"  v-if="checkPermi(['imgentity:index:delete'])" size="small" @click="deleteHandle(scope.row.imgid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="tableForm.pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="tableForm.pageSize"
      :total="tableForm.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" ></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './imgentity-add-and-update'
  import * as api from '@/api/imgentity'
  import {checkPermi} from "@/utils/permission";
  import * as categoryApi from "@/api/categoryApi";

  export default {
    data () {
      return {
        dataList: [],
        queryForm: {
          imgid: ''
        },
        tableForm:{
          pageIndex: 1,
          pageSize: 10,
          total: 0,
        },
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    mounted () {
      this.getDataList()
    },
    methods: {
      checkPermi,
      search(){
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataListLoading = true
        let postData={}
        Object.assign(postData,this.queryForm, this.tableForm)
        //postData.key=postData.key.length==0?postData.key:'%'+postData.key+'%'
        api.list(postData).then(res => {
          this.dataList=res.datalist
          this.tableForm.total=res.total
        }).finally(() => {
          this.dataListLoading = false
        });
      },
      // 每页数
      sizeChangeHandle (val) {
        this.tableForm.pageSize = val
        this.tableForm.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.tableForm.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.imgid
        })
        this.$confirm(`您确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let postData={ids:ids.join(',')};
          api.deleteByIds(postData).then(res => {
            this.$message.success("删除成功")
            this.getDataList()
          })
        })
      }
    }
  }
</script>
